package guiElements;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class FabricSearchPanelMain extends JPanel {

	private NameSearchPanel nameSearchPanel;
	private ColorSelectorPanel colorSelectorPanel;
	private PatternSelectorPanel patternSelectorPanel;

	private JPanel submitPanel;
	private JButton submitButton, resetButton;

	public FabricSearchPanelMain() {

		nameSearchPanel = new NameSearchPanel();
		colorSelectorPanel = new ColorSelectorPanel();
		patternSelectorPanel = new PatternSelectorPanel();

		submitPanel = new JPanel();
		submitPanel.setLayout(null);
		submitPanel.setBackground(Color.white);
		submitButton = new JButton("Search");
		submitButton.setSize(new Dimension(100, 25));
		submitButton.setLocation(new Point(375, 10));
		resetButton = new JButton("Reset");
		resetButton.setSize(new Dimension(100, 25));
		resetButton.setLocation(new Point(225, 10));
		submitPanel.add(submitButton);
		submitPanel.add(resetButton);

		setLayout(null);
		setBackground(Color.white);
		nameSearchPanel.setSize(new Dimension(550, 25));
		nameSearchPanel.setLocation(new Point(0, 0));

		colorSelectorPanel.setSize(new Dimension(550, 250));
		colorSelectorPanel.setLocation(new Point(0, 40));

		patternSelectorPanel.setSize(new Dimension(550, 200));
		patternSelectorPanel.setLocation(new Point(0, 290));

		submitPanel.setSize(new Dimension(550, 50));
		submitPanel.setLocation(new Point(0, 500));

		add(nameSearchPanel);
		add(colorSelectorPanel);
		add(patternSelectorPanel);
		add(submitPanel);

	}

	public ArrayList<Fabric> fabricSearch(ArrayList<Fabric> fabricData) {
		boolean textMatch = false, colorMatch = false, patternMatch = false;
		ArrayList<Fabric> outData = new ArrayList<Fabric>(0);
		ArrayList<Fabric> textFiltered = new ArrayList<Fabric>(0);
		ArrayList<Fabric> colorFiltered = new ArrayList<Fabric>(0);
		ArrayList<Fabric> patternFiltered = new ArrayList<Fabric>(0);
		ArrayList<String> colorsSelected = colorSelectorPanel
				.getSelectedColors();
		ArrayList<String> patternsSelected = patternSelectorPanel
				.getSelectedPatterns();
		String keywordString = nameSearchPanel.getNameTextField().getText();
		Scanner scan = new Scanner(keywordString);
		scan.useDelimiter(" ");
		ArrayList<String> keywordList = new ArrayList<String>(0);
		while (scan.hasNext()) {
			keywordList.add(scan.next());
		}
		scan.close();
		outData = fabricData;
		// Text Loop
		for (Fabric fab : outData) {
			boolean add = false;
			if (!keywordList.isEmpty()) {
				for (String word : keywordList) {
					for (String nameWord : fab.getWords()) {
						if (word.equalsIgnoreCase(nameWord)) {
							add = true;
							break;
						}
					}
				}
				if (add) {
					textFiltered.add(fab);
				}
			}
		}
		if (!textFiltered.isEmpty()) {
			outData = textFiltered;
			textMatch = true;
		} else

			// Color Loop
			for (Fabric fab : outData) {
				boolean add = false;
				if (!colorsSelected.isEmpty()) {
					for (String currentColor : colorsSelected) {
						if (currentColor
								.equalsIgnoreCase(fab.getPrimaryColor())) {
							add = true;
							break;
						}
						if (currentColor.equalsIgnoreCase(fab
								.getSecondaryColor())) {
							add = true;
							break;
						}
						if (currentColor.equalsIgnoreCase(fab
								.getTertiaryColor())) {
							add = true;
							break;
						}
					}
					if (add) {
						colorFiltered.add(fab);
					}
				}
			}
		if (!colorFiltered.isEmpty()) {
			outData = colorFiltered;
			colorMatch = true;
		}

		// Pattern Loop
		for (Fabric fab : outData) {
			boolean add = false;
			if (!patternsSelected.isEmpty()) {
				for (String currentPattern : patternsSelected) {
					if (currentPattern.equalsIgnoreCase(fab.getPatternOne())) {
						add = true;
						break;
					}
					if (currentPattern.equalsIgnoreCase(fab.getPatternTwo())) {
						add = true;
						break;
					}
				}
				if (add) {
					patternFiltered.add(fab);
				}
			}
		}
		if (!patternFiltered.isEmpty()) {
			outData = patternFiltered;
			patternMatch = true;
		}
		if (!colorMatch && !patternMatch && !textMatch) {
			JOptionPane chooseOne = new JOptionPane(
					"No Matches Found. Please Refine Your Search.",
					JOptionPane.WARNING_MESSAGE);
			JDialog chooseOneD = chooseOne.createDialog("Search Error");
			chooseOneD.setVisible(true);
			outData = new ArrayList<Fabric>();
		}
		return outData;
	}

	public void fabricReset() {
		nameSearchPanel.reset();
		colorSelectorPanel.reset();
		patternSelectorPanel.reset();
	}

	public NameSearchPanel getNameSearchPanel() {
		return nameSearchPanel;
	}

	public void setNameSearchPanel(NameSearchPanel nameSearchPanel) {
		this.nameSearchPanel = nameSearchPanel;
	}

	public ColorSelectorPanel getColorSelectorPanel() {
		return colorSelectorPanel;
	}

	public void setColorSelectorPanel(ColorSelectorPanel colorSelectorPanel) {
		this.colorSelectorPanel = colorSelectorPanel;
	}

	public PatternSelectorPanel getPatternSelectorPanel() {
		return patternSelectorPanel;
	}

	public void setPatternSelectorPanel(
			PatternSelectorPanel patternSelectorPanel) {
		this.patternSelectorPanel = patternSelectorPanel;
	}

	public JPanel getSubmitPanel() {
		return submitPanel;
	}

	public void setSubmitPanel(JPanel submitPanel) {
		this.submitPanel = submitPanel;
	}

	public JButton getSubmitButton() {
		return submitButton;
	}

	public void setSubmitButton(JButton submitButton) {
		this.submitButton = submitButton;
	}

	public JButton getResetButton() {
		return resetButton;
	}

	public void setResetButton(JButton resetButton) {
		this.resetButton = resetButton;
	}

}